package id.link.win.login.sample.configuration;

import net.sourceforge.spnego.SpnegoHttpFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.annotation.Order;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: Foy Lian
 * Date: 8/1/2019
 * Time: 3:59 PM
 */
@Configuration
public class SpnegoConfig {

    @Bean
    @Order(1)
    @Profile("dev")
    public FilterRegistrationBean<SpnegoHttpFilter> loggingFilter(){
        FilterRegistrationBean<SpnegoHttpFilter> registrationBean
                = new FilterRegistrationBean<>();

        registrationBean.setFilter(new SpnegoHttpFilter());
        //TODO 考虑从配置文件读取
        //copy from http://spnego.sourceforge.net/spnego_tomcat.html
        registrationBean.addInitParameter("spnego.allow.basic","false");
        registrationBean.addInitParameter("spnego.allow.localhost","false");
        registrationBean.addInitParameter("spnego.allow.unsecure.basic","false");
        registrationBean.addInitParameter("spnego.login.client.module","spnego-client");

        // need use relative path, classpath not work?
        //registrationBean.addInitParameter("spnego.krb5.conf","classpath:/krb5.conf");
        //registrationBean.addInitParameter("spnego.login.conf","classpath:/login.conf");
        //这好像只能用绝对路径～～～～ 回头看看有办法改没。。
        registrationBean.addInitParameter("spnego.krb5.conf","d:/tmp/krb5.conf");
        registrationBean.addInitParameter("spnego.login.conf","d:/tmp/login.conf");

        //FIXME should use pre auth account not admin.. 不应该用管理员，pre auth是什么账户？？
        registrationBean.addInitParameter("spnego.preauth.username","zeus");
        registrationBean.addInitParameter("spnego.preauth.password","zrkj123.");

        registrationBean.addInitParameter("spnego.login.server.module","spnego-server");
        registrationBean.addInitParameter("spnego.prompt.ntlm","false");
        registrationBean.addInitParameter("spnego.logger.level","1");

        registrationBean.addServletNames("SpnegoHttpFilter");
        registrationBean.addUrlPatterns("/spnego/*");

        return registrationBean;
    }
}
